package com.yang.doctorrecruitment.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.doctorrecruitment.dao.ApplyDAO;
import com.yang.doctorrecruitment.dto.Apply;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class UserApplyAction extends ActionSupport {
    private JSON json;
    public String UserApply(){
//        得到参数，
        Map session=  ActionContext.getContext().getSession();
        String UserID= (String) session.get("UserID");
        HttpServletRequest request= (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String JobID=request.getParameter("JobID");
        int ResumeKind= Integer.parseInt(request.getParameter("ResumeKind"));
        String ResumeAddress=request.getParameter("ResumeAddress");
        Date date=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String ResumeTime=df.format(new Date());
        Apply apply=new Apply();
        apply.setUserID(UserID);
        apply.setJobID(JobID);
        apply.setResumeKind(ResumeKind);
        apply.setResumeTime(ResumeTime);
        apply.setReply("");
        //还需要先做一个查重判断，就是看用户是否已经投递了该份招聘！
        if(ResumeKind==1){
            //表示如果是默认简历怎么处理
            apply.setResumeAddress("");
        }else{
            apply.setResumeAddress(ResumeAddress);
        }
        JSONObject object=new JSONObject();
       boolean exist=ApplyDAO.ApplyExist(apply);
       if(exist){
           object.put("result","-1");
           //表示已经存在，已经投递过
       }else{
           boolean res= ApplyDAO.InsertApply(apply);
           if(res){
               object.put("result","1");
               //投递成功
           }else{
               object.put("result","0");
               //投递失败
             }

       }
        json=object;
        return SUCCESS;
    }

    public JSON getJson() {
        return json;
    }

    public void setJson(JSON json) {
        this.json = json;
    }
}
